package com.yky.action.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.yky.action.base.BaseActionSupport;
import com.yky.entity.Organization;
import com.yky.entity.Province;
import com.yky.entity.User;
import com.yky.service.admin.OrganizationService;
import com.yky.service.admin.ProvinceService;
import com.yky.service.admin.UserService;
import com.yky.utils.Cn2Spell;
import com.yky.utils.JsonUtil;
import com.yky.utils.base.MD5;

/**
 * 用户管理
 * 
 * @author vimesly
 * 
 */
@ParentPackage("perInterceptor")
@InterceptorRefs( { @InterceptorRef("sessionStack") })
@Namespace(value = "/admin")
@Results( {
		@Result(name = "user_regsucess", type = "redirect", location = "/admin/user/user_list.jsp"),
		@Result(name = "user_edit", location = "/admin/user/user_edit.jsp"),
		@Result(name = "user_audit", type = "redirect", location = "/admin/user/user_audit.jsp") })
public class UserAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;

	@Autowired
	private UserService userService;

	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private OrganizationService organizationService;

	private String searchText;

	private Integer state;

	protected String id;
	private String pageIndex;
	private String pageShowCount;

	private String username;
	private String name;
	private String password;
	private String reason;

	private String orgname;
	private String province;
	
	private String auditsState;
	private String typeid;
	private String lianghui;
	private String shejiren;

	private String ypassword;

	public String updataPersonageInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println("password=" + password);
		System.out.println("ypassword=" + ypassword);
		User user = (User) getSession().getAttribute("user");
		User currentUser = userService.find(user.getId());
		if(!ypassword.equals(currentUser.getPassword())) {
			request.setAttribute("info", "原密码错误，请重新输入");
		}else {
			currentUser.setPassword(password);
			userService.updateEntity(currentUser);
			request.setAttribute("info", "修改密码成功!");
		}
		return "user_edit";
	}

	// 省级用户初始化操作
	public String initUserList() {
		try {
			List<Province> list = provinceService.findEntityList();
			for (int i = 0; i < list.size(); i++) {
				Province aProvince = list.get(i);
				String shortName = aProvince.getShortName();
				String username = "manager_"
						+ Cn2Spell.converterToSpell(shortName);
				String password = MD5.convert("123456");
				String name = "省级管理员-" + shortName;
				int roleType = 1;
				User aUser = new User();
				aUser.setName(name);
				aUser.setPassword(password);
				aUser.setUsername(username);
				aUser.setRoleType(roleType);
				aUser.setProvince(aProvince);
				userService.saveEntity(aUser);
				System.out.println(username + "||| 新增成功！");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

	public String getProvinceList() throws Exception {
		List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
		List<Object[]> orglist = provinceService
				.findBySQL("SELECT province_code as province,COUNT(province_code) as count FROM [dbo].[sys_user] as users Where users.status=1 GROUP BY province_code");

		Map<Object, Object> provMap = new HashMap<Object, Object>();
		if (null != orglist && orglist.size() > 0) {
			for (Object[] numObj : orglist) {
				if (null != numObj[0]) {
					provMap.put(numObj[0], numObj[1]);
				}
			}
		}

		List<Province> provlist = provinceService.findEntityList();
		for (Province province : provlist) {
			Map<Object, Object> m = new HashMap<Object, Object>();
			m.put("name", province.getName());
			m.put("code", province.getCode());
			if (provMap.containsKey(province.getCode())) {
				m.put("num", provMap.get(province.getCode()));
			} else {
				m.put("num", "0");
			}
			list.add(m);
		}
		super.pringWriterToPage(JsonUtil.formatToJson(list));
		return null;
	}
	public String getAuditsProvinceList() throws Exception {
		List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
		List<Object[]> orglist = provinceService
				.findBySQL("SELECT province_code as province,COUNT(province_code) as count FROM [dbo].[sys_user] as users Where users.role_type=2 GROUP BY province_code");

		Map<Object, Object> provMap = new HashMap<Object, Object>();
		if (null != orglist && orglist.size() > 0) {
			for (Object[] numObj : orglist) {
				if (null != numObj[0]) {
					provMap.put(numObj[0], numObj[1]);
				}
			}
		}

		List<Province> provlist = provinceService.findEntityList();
		for (Province province : provlist) {
			Map<Object, Object> m = new HashMap<Object, Object>();
			m.put("name", province.getName());
			m.put("code", province.getCode());
			if (provMap.containsKey(province.getCode())) {
				m.put("num", provMap.get(province.getCode()));
			} else {
				m.put("num", "0");
			}
			list.add(m);
		}
		super.pringWriterToPage(JsonUtil.formatToJson(list));
		return null;
	}

	public String saveUser() throws Exception {
		try {
			User userS = (User) getSession().getAttribute("user");
			User user = new User();
			user.setUsername(username);
			user.setName(name);
			user.setPassword(password);
			user.setRoleType(3);
			user.setStatus(1);
			user.setProvince(userS.getProvince());
			user.setOrganization(userS.getOrganization());
			userService.saveEntity(user);

			return "user_regsucess";

		} catch (Exception e) {
			e.printStackTrace();
			super.pringWriterToPage("error");
		}

		return null;
	}

	public String updataUser() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		User sessionUser = (User) request.getSession(false)
				.getAttribute("user");
		try {
			if (null != id && id.length() > 0) {
				User user = userService.find(Integer.parseInt(id));
				if (password != null && !"".equals(password)) {
					user.setPassword(password);
					userService.updateEntity(user);
				}
			}
			if (sessionUser.getRoleType() == 1) {
				return "user_audit";
			} else {
				return "user_regsucess";
			}

		} catch (Exception e) {
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}

	public String findUser() throws Exception {
		try {
			if (null != id && id.length() > 0) {
				User user = userService.find(Integer.parseInt(id));
				String str = JsonUtil.formatToJson(user);
				super.pringWriterToPage(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}

	public String findUserEdit() throws Exception {
		try {
			User userS = (User) getSession().getAttribute("user");
			String str = JsonUtil.formatToJson(userS);
			super.pringWriterToPage(str);
		} catch (Exception e) {
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}

	public String saveUserPass() throws Exception {
		try {
			if (null != id && id.length() > 0) {
				User user = userService.find(Integer.parseInt(id));
				user.setStatus(1);
				user.setReason("");
				userService.updateEntity(user);
				// 同时修改该用户的机构状态
				Organization organization = organizationService.findUniqueBy(
						"createUser", user.getUsername());
				organization.setStatus(1);
				organizationService.updateEntity(organization);
				super.pringWriterToPage("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}

	public String saveUserReason() throws Exception {
		try {
			if (null != id && id.length() > 0) {
				User user = userService.find(Integer.parseInt(id));
				user.setStatus(2);
				user.setReason(reason);
				userService.updateEntity(user);
				super.pringWriterToPage("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}

	public void deleteUser() {
		try {
			userService.deleteEntity(Integer.parseInt(id));
			super.printWriterJsonToPage("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getPageCount() throws Exception {
		// TODO Auto-generated method stub
		if (searchText == null || "请输入名称".equals(searchText)) {
			searchText = "";
		}
		if (orgname == null || "请输入机构名".equals(orgname)) {
			orgname = "";
		}
		int count = searchUsers(searchText, orgname);
		String content = JsonUtil.formatToJson(count);
		super.pringWriterToPage(content);
		return null;
	}

	public int searchUsers(String sql, String orgname) {
		User userS = (User) getSession().getAttribute("user");
		int result = 0;
		List<User> list = new ArrayList<User>();

		String quryS = "";
		Object[] object = new Object[] {};
			if (state == 1) {
				if (null != userS.getProvince()) {
					if (userS.getRoleType() == 2) {
						if("".equals(lianghui)){
							if("".equals(shejiren)){
								quryS = "o.username like ? and o.organization.id=? and o.roleType=?";
							}else{
								quryS = "o.username like ? and o.organization.id=? and o.roleType=? and o.organization.isInvolvedPerson="+shejiren;
							}
						}else{
							if("".equals(shejiren)){
								quryS = "o.username like ? and o.organization.id=? and o.roleType=? and o.organization.isNationalTwoCommittees="+ lianghui;
							}else{
								quryS = "o.username like ? and o.organization.id=? and o.roleType=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
							}
						}
						//quryS = "o.username like ? and o.organization.id=? and o.roleType=? ";
						object = new Object[] { "%" + sql + "%",userS.getOrganization().getId(), 3 };
						
					} else if (userS.getRoleType() == 1) {
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? ";
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
						object = new Object[] { "%" + sql + "%",
								userS.getProvince().getCode(), 1 };
					}
				} else {
					if (null != province && province.length() > 0) {
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? ";
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
						object = new Object[] { "%" + sql + "%", province, 1 };
					} else {
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? ";
								}else{
									quryS = "o.username like ? and o.status=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
						object = new Object[] { "%" + sql + "%", 1 };
					}
				}
			} else if (state == 2) {
				if(!"admin".equals(userS.getUsername())){
					if("-1".equals(auditsState)){
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=?" ;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code=? and o.organization.isInvolvedPerson="+shejiren ;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}else{
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? ";// and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}
					object = new Object[] { "%" + sql + "%",
							userS.getProvince().getCode()/*, 2 */};
				}else{
					String p = "0";
					if (null != province && province.length() > 0) {
						p=province;
					}
					if("-1".equals(auditsState)){
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ?" ;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.organization.isInvolvedPerson="+shejiren ;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}else{
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? ";// and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid+" and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}
					object = new Object[] { "%" + sql + "%","%"+p+"%"/*, 2*/ };
				}
			}
		try {
			list = userService.findByPropertys(quryS, object);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (null != list) {
			result = Integer.valueOf(String.valueOf(list.size()));
		}
		return result;
	}

	public String getPageList() throws Exception {
		// TODO Auto-generated method stub
		if (searchText == null || "请输入名称".equals(searchText)) {
			searchText = "";
		}
		if (orgname == null || "请输入机构名".equals(orgname)) {
			orgname = "";
		}
		Map<Object, Object> resultMap = new HashMap<Object, Object>();
		List<User> list = null;
		try {

			int page = Integer.valueOf(pageIndex);
			int maxResult = Integer.valueOf(pageShowCount);
			int firstResult = maxResult * page;

			list = searchUserList(searchText, orgname, firstResult, maxResult);
			if (null != list) {
				List<Map<Object, Object>> result = new ArrayList<Map<Object, Object>>();
				User userS = (User) getSession().getAttribute("user");

				Map<Object, Object> map = null;
				for (User user : list) {
					map = new HashMap<Object, Object>();
					map.put("id", user.getId());
					map.put("username", user.getUsername());
					map.put("name", user.getName());
					map.put("status", user.getStatus());
					map.put("roleType", user.getRoleType());
					if (null != user.getOrganization()) {
						map.put("organization", user.getOrganization().getName());
						map.put("isInvolvedPerson", user.getOrganization().getIsInvolvedPerson());
						map.put("isNationalTwoCommittees", user.getOrganization().getIsNationalTwoCommittees());
					} else {
						map.put("organization", "");
					}
					if (null != user.getProvince()) {
						map.put("province", user.getProvince().getName());
					} else {
						map.put("province", "");
					}

					result.add(map);
				}
				resultMap.put("datalist", result);
				resultMap.put("state", userS.getRoleType());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String content = JsonUtil.formatMapToJson(resultMap);
		super.pringWriterToPage(content);
		return null;
	}

	public List<User> searchUserList(String sql, String orgname,
			int firstResult, int maxResult) {
		User userS = (User) getSession().getAttribute("user");
		List<User> list = new ArrayList<User>();
		LinkedHashMap<String, String> mp = new LinkedHashMap<String, String>();
		mp.put("status", "asc");
		String quryS = "";
		Object[] object = new Object[] {};
			if (state == 1) {
				if (null != userS.getProvince()) {
					if (userS.getRoleType() == 2) {
						if("".equals(lianghui)){
							if("".equals(shejiren)){
								quryS = "o.username like ? and o.organization.id=? and o.roleType=?";
							}else{
								quryS = "o.username like ? and o.organization.id=? and o.roleType=? and o.organization.isInvolvedPerson="+shejiren;
							}
						}else{
							if("".equals(shejiren)){
								quryS = "o.username like ? and o.organization.id=? and o.roleType=? and o.organization.isNationalTwoCommittees="+ lianghui;
							}else{
								quryS = "o.username like ? and o.organization.id=? and o.roleType=? and o.organization.isNationalTwoCommittees="+ lianghui+" and o.organization.isInvolvedPerson="+shejiren;
							}
						}
						//quryS = "o.username like ? and o.organization.id=? and o.roleType=? ";
						object = new Object[] { "%" + sql + "%",
								userS.getOrganization().getId(), 3 };
					} else if (userS.getRoleType() == 1) {
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? ";
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
						object = new Object[] { "%" + sql + "%",
								userS.getProvince().getCode(), 1 };
					}
				} else {
					if (null != province && province.length() > 0) {
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? ";
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
						object = new Object[] { "%" + sql + "%", province, 1 };
					} else {
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? ";
								}else{
									quryS = "o.username like ? and o.status=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.status=? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.status=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
						object = new Object[] { "%" + sql + "%", 1 };
					}
				}
			} else if (state == 2) {
				if(!"admin".equals(userS.getUsername())){
					if("-1".equals(auditsState)){
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=?" ;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code=? and o.organization.isInvolvedPerson="+shejiren ;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}else{
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? ";// and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code=? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}
					object = new Object[] { "%" + sql + "%",userS.getProvince().getCode()};//, 2 
				}else{
					String p = "0";
					if (null != province && province.length() > 0) {
						p=province;
					}
					if("-1".equals(auditsState)){
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ?" ;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.organization.isInvolvedPerson="+shejiren ;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid;
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.username like ? and o.province.code like ? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}else{
						if("".equals(typeid)){
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? ";// and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui;//and o.roleType=?
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}else{
							if("".equals(lianghui)){
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}else{
								if("".equals(shejiren)){
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui;
								}else{
									quryS = "o.status="+auditsState+" and o.username like ? and o.province.code like ? and o.roleType="+typeid+"and o.organization.isNationalTwoCommittees="+ lianghui+"and o.organization.isInvolvedPerson="+shejiren;
								}
							}
						}
					}
					object = new Object[] { "%" + sql + "%","%"+p+"%" };//, 2
				}
			}
		list = userService.findByPropertys(firstResult, maxResult, quryS,
				object, mp);

		return list;
	}

	public ProvinceService getProvinceService() {
		return provinceService;
	}

	public void setProvinceService(ProvinceService provinceService) {
		this.provinceService = provinceService;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getPageShowCount() {
		return pageShowCount;
	}

	public void setPageShowCount(String pageShowCount) {
		this.pageShowCount = pageShowCount;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getOrgname() {
		return orgname;
	}

	public void setOrgname(String orgname) {
		this.orgname = orgname;
	}

	public String getYpassword() {
		return ypassword;
	}

	public void setYpassword(String ypassword) {
		this.ypassword = ypassword;
	}

	public String getAuditsState() {
		return auditsState;
	}

	public void setAuditsState(String auditsState) {
		this.auditsState = auditsState;
	}

	public String getTypeid() {
		return typeid;
	}

	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}

	public String getLianghui() {
		return lianghui;
	}

	public void setLianghui(String lianghui) {
		this.lianghui = lianghui;
	}

	public String getShejiren() {
		return shejiren;
	}

	public void setShejiren(String shejiren) {
		this.shejiren = shejiren;
	}

	
}
